package com.hyj.ecp.clientservice.order.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;

import com.ecp.order.OrderInfoGoods;
import com.ecp.order.OrderQO;
import com.hyj.ecp.clientservice.exception.DAOException;
import com.hyj.ecp.clientservice.order.service.IOrderService;
@Service
public class OrderService implements IOrderService {

	@Resource(name="sqlSession")
    public  SqlSessionTemplate sql;

	@Override
	public OrderQO saveOrder(OrderQO qo) throws DAOException {
		
       
		// 货币购买
		if("1".equals(qo.getType())){
			// 1  增加订单表 以及订单信息表
			int a =  sql.insert("com.ecp.client.order.saveOrder",qo);
			int point = 0;
			int orderId = 0;
			if (a > 0) {
			    orderId = sql.selectOne("com.ecp.client.order.lastInsertId");
				for (OrderInfoGoods vo : qo.getGoods()) {
					vo.setOrderId(orderId);
					point = point + vo.getPoint()*vo.getNum();
					a = sql.insert("com.ecp.client.order.insertOrderGoods", vo);
					if (a <= 0) {
						throw new DAOException("订单提交失败");
					}
					
					//  商品表销售量   增加
					a = sql.update("updateGoodsSail",vo);
					/*//    商品属性表库存 减少
					a =  sql.update("updateGoodsNum",vo);*/
					if(a<=0){
						throw new DAOException("库存不够！");
					}
					
				}
				
			// 第二步： 增加客户积分表
				qo.setPoint(point);
				int num = sql.selectOne("com.ecp.client.order.queryCustPoint",qo);
				if(num>0){// 存在就更新，增加积分
					sql.update("com.ecp.client.order.updateCustPoint", qo);
				}else{//首次购物  插入积分表
					sql.insert("com.ecp.client.order.insertCustPoint", qo);
				}
				
			/*	String strs =	sql.getConfiguration().getMappedStatement("com.ecp.client.order.saveOrder").getBoundSql(qo).getSql();
				System.out.println(strs);*/
				
			}
			qo.setId(orderId);
		    return qo;
		}else if("2".equals(qo.getType())){//积分订单
			// 1  增加订单表 以及订单信息表
			int a =  sql.insert("com.ecp.client.order.saveOrder",qo);
			int price = 0;
			int orderId = 0;
			if (a > 0) {
			    orderId = sql.selectOne("com.ecp.client.order.lastInsertId");
				for (OrderInfoGoods vo : qo.getGoods()) {
					vo.setOrderId(orderId);
					price = price + vo.getPrice()*vo.getNum();
					a = sql.insert("com.ecp.client.order.insertOrderGoods", vo);
					if (a <= 0) {
						throw new DAOException("订单提交失败");
					}
					
					//  商品表销售量   增加
					a = sql.update("updateGoodsSail",vo);
					/*//    商品属性表库存 减少
					a =  sql.update("updateGoodsNum",vo);*/
					if(a<=0){
						throw new DAOException("库存不够！");
					}
					
				}
				
			// 第二步： 减少客户积分表
				qo.setPrice(price);
				int custPoint = sql.selectOne("com.ecp.client.order.queryCustPointVal",qo);//用户可用积分
				if(custPoint>=price){// 如果可用积分大于兑换所需要积分
					sql.update("com.ecp.client.order.updateCustPoint2", qo); //减少客户积分
				}else{
				  throw new DAOException("用户积分不足！");
				}
				
				
			}
			qo.setId(orderId);
		    return qo;
		}else {
			return null;
		}
	}

	@Override
	public List<Map<String, Object>> queryOrderList(OrderQO qo) throws DAOException {
		
		List<Map<String, Object>> list = sql.selectList("com.ecp.client.order.queryOrderList", qo);
		return list;
	}

	@Override
	public Map<String, Object> queryOrderInfo(OrderQO qo) throws DAOException {
		Map<String, Object> order = sql.selectOne("com.ecp.client.order.queryOrderList", qo);
		
		List<Map<String, Object>> list = sql.selectList("com.ecp.client.order.queryOrderInfo", qo);
		Map<String, Object>  map = new HashMap<String, Object>();
		map.put("goods", list);
		map.put("id", order.get("ID"));
		map.put("price", order.get("PRICE"));
		map.put("status", order.get("STATUS"));
		map.put("time", order.get("ADDTIME"));
		return map;
	}

	@Override
	public int updateAliSecurity(String req) throws DAOException {
		
		int a = sql.insert("com.ecp.client.order.insertZFBlog", req);
		
		// 跟新订单状态
		
		return a;
	}
}
